<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div class="container">
        <h2 class="page-header">promise初体验</h2>
        <button class="btn btn-primary" id="btn">点击抽奖</button>
    </div>

    <script>
        function rand(m,n){
            return Math.ceil(Math.random()*(n-m+1)+m-1);
        }

        const btn = document.querySelector("#btn");
        btn.addEventListener("click",function(){
            /* setTimeout(() => {
                let n = rand(1,100);
                if(n <= 30){
                    alert("恭喜");
                }else{
                    alert("再接再厉");
                }
            }, 1000); */

            const p = new Promise((resolve,reject)=>{
                setTimeout(() => {
                    let n = rand(1,100);
                    if(n <= 30){
                        resolve(n);
                    }else{
                        reject(n);
                    }
                }, 1000);
            });

            p.then((value)=>{
                alert("恭喜,您的中将数字为"+value);
            },(reason)=>{
                alert("再接再厉,您的号码为"+reason);
            });
        });
    </script>
</body>
</html>